'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 进入战斗
 * @author sundbcn@gmail.com (sun debing)
 */

var async = require('async');
var Code = require('../../../../consts/code');
var atkData = require('../../../../libs/war/atkData');
var unionBossData = require('../../../../libs/war/unionBossData');
var constantUtil = require('../../../../util/constantUtil');
var PlayerManager = require('../../../../libs/playerManager');
var taskService = require('../../../../services/taskService.js');
var unionGroupModel = require('../../../../models/unionGroupModel.js');
var unionBossModel = require('../../../../models/unionBossModel.js');
var unionBossConf = require('../../../../config/unionBossConf.js');

module.exports = function (app) {
    return new Handler(app);
};

var Handler = function (app) {
    this.app = app;
};

/**
 * 进入战斗
 */
module.exports = function (msg, session, next) {
    var role_id = session.uid;

    var role_model;
    var boss_id;
    var atk_data;
    var def_data;

    var union_group_model;
    var union_boss_model;
    var union_id;

    var player_manager = new PlayerManager(role_id);
    async.waterfall(
        [
            function (cb) {
                player_manager.refreshData(cb);
            },
            function (cb) {
                player_manager.getModel(['role', 'role_union_boss', 'task', 'daily_task', 'achieve', 'sevenDayActivity', 'accrued'], cb);
            },
            function (cb) {
                role_model = player_manager.role;
                union_id = role_model.union_id;
                if (union_id === 0) {
                    return next(null, {
                        code: Code.UNION_IS_NOT_JOIN
                    });
                }

                // 联盟信息
                unionGroupModel.getById(union_id, cb);
            },
            function (model, cb) {
                union_group_model = model;

                // 联盟BOSS
                unionBossModel.findById(union_id, cb);
            },
            function (model, cb) {
                union_boss_model = model;

                boss_id = union_boss_model.curr_boss_id;
                var boss_conf = unionBossConf.getUnionBossConf(boss_id);
                if (!boss_conf) {
                    return next(null, {
                        code: Code.UNIOIN_BOSS_INVALID_BOSS_ID
                    });
                }

                if (boss_conf.open_lvl > union_group_model.lv) {
                    return next(null, {
                        code: Code.UNIOIN_BOSS_NO_FIGHTABLE_BOSS
                    });
                }

                // 增加攻打次数统计
                taskService.addCompleteUnionBoos(player_manager);

                // 锁定BOSS
                var role_name = role_model.name;
                unionBossModel.lockBoss(union_id, role_id, role_name, cb);
            },
            function (success, cb) {
                if (!success) {
                    return next(null, {
                        code: Code.UNIOIN_BOSS_IN_BATTLE
                    });
                }

                // 扣减攻打次数
                var role_union_boss_model = player_manager.role_union_boss;
                if (!role_union_boss_model.decRemainTimes()) {
                    unionBossModel.unLockBoss(union_id, function (err) {
                        return next(null, {
                            code: Code.UNIOIN_BOSS_NO_BATTLE_TIMES
                        });
                    });
                    return;
                }

                def_data = _getDef(boss_id);
                def_data.boss_list[0].attr.curr_hp = union_boss_model.curr_boss_hp;

                // 我方数据
                _getAtk(role_id, cb);
            },
            function (data, cb) {
                atk_data = data;

                player_manager.save(cb);
            }
        ],
        function (err) {
            if (!!err) {
                console.error(err);
                unionBossModel.unLockBoss(union_id, function (err) {
                    return next(null, {
                        code: Code.FAIL
                    });
                });
                return;
            }
            return next(null, {
                code: Code.OK,
                result: {
                    record_id: boss_id,
                    atk: atk_data,
                    def: def_data
                }
            });
        }
    );
};

/**
 * 获取攻击方数据
 * @return {[type]} [description]
 */
var _getAtk = function (role_id, cb) {
    atkData.getData(role_id, cb);
};

/**
 * 获取防守方数据
 * @return {[type]} [description]
 */
var _getDef = function (boss_id) {
    return unionBossData.getData(boss_id);
};
